import 'package:json_annotation/json_annotation.dart';

part 'category_model.g.dart';

/// 分类模型
/// 用于管理交易分类（收入/支出）
@JsonSerializable()
class CategoryModel {
  /// 分类ID
  final String id;

  /// 分类名称
  final String name;

  /// 分类图标（emoji或icon名称）
  final String icon;

  /// 分类类型（expense: 支出, income: 收入）
  final String type;

  /// 分类颜色（十六进制颜色值）
  final String color;

  /// 是否为默认分类
  @JsonKey(name: 'is_default')
  final bool isDefault;

  /// 构造函数
  CategoryModel({
    required this.id,
    required this.name,
    required this.icon,
    required this.type,
    required this.color,
    required this.isDefault,
  });

  /// 从JSON创建对象
  factory CategoryModel.fromJson(Map<String, dynamic> json) =>
      _$CategoryModelFromJson(json);

  /// 转换为JSON
  Map<String, dynamic> toJson() => _$CategoryModelToJson(this);

  /// 复制并更新属性
  CategoryModel copyWith({
    String? id,
    String? name,
    String? icon,
    String? type,
    String? color,
    bool? isDefault,
  }) {
    return CategoryModel(
      id: id ?? this.id,
      name: name ?? this.name,
      icon: icon ?? this.icon,
      type: type ?? this.type,
      color: color ?? this.color,
      isDefault: isDefault ?? this.isDefault,
    );
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is CategoryModel &&
          runtimeType == other.runtimeType &&
          id == other.id;

  @override
  int get hashCode => id.hashCode;
}
